package cn.xsw.controller;


import cn.xsw.common.ResultBody;
import cn.xsw.entity.Article;
import cn.xsw.entity.query.ArticleQuery;
import cn.xsw.entity.query.CommentQuery;
import cn.xsw.entity.vo.ArticleVo;
import cn.xsw.service.ArticleCategoryService;
import cn.xsw.service.ArticleService;
import cn.xsw.service.impl.CommentServiceImpl;
import cn.xsw.utils.BaseQuery;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liangYiHui
 * @since 2021-05-31
 */
@RestController
@RequestMapping("/article")
@Api(tags = "文章接口")
public class ArticleController{
    @Resource
    private ArticleService articleService;
    @Resource
    private CommentServiceImpl commentService;
    @Resource
    private ArticleCategoryService articleCategoryService;


    @ApiOperation("根据id查询")
    @GetMapping("/findById/{id}")
    public ResultBody findById(@PathVariable("id") int id){
        Article article = articleService.getById(id);
        ArticleVo articleVo = new ArticleVo();
        BeanUtils.copyProperties(article,articleVo);
        articleVo.setCategory(articleCategoryService.getById(article.getCid()));
        articleVo.setCommentVos(commentService.findPageVo(new CommentQuery().setAid(article.getId())).getRecords());
        return ResultBody.success(articleVo);
    }

    @ApiOperation("分页查询，默认查询前5条评论")
    @PostMapping("/findPage")
    public ResultBody findPage(@RequestBody ArticleQuery query){
        Page page = articleService.page(query.page(),query.wrapper());
        List<Article> articles = page.getRecords();
        List<ArticleVo> articleVos = new ArrayList<>();
        articles.forEach(article -> {
            ArticleVo articleVo = new ArticleVo();
            BeanUtils.copyProperties(article,articleVo);
            articleVo.setCategory(articleCategoryService.getById(article.getCid()));
            articleVo.setCommentVos(commentService.findPageVo(new CommentQuery().setAid(article.getId())).getRecords());
            articleVos.add(articleVo);
        });
        page.setRecords(articleVos);
        return ResultBody.success().page(page);
    }

    @ApiOperation("查询主页文章")
    @GetMapping("/findIndex")
    public ResultBody findIndex(){
        List<Article> articles = articleService.list(new QueryWrapper<Article>().eq("is_index", 1));
        List<ArticleVo> articleVos = new ArrayList<ArticleVo>();
        articles.forEach(article -> {
            ArticleVo articleVo = new ArticleVo();
            BeanUtils.copyProperties(article,articleVo);
            articleVo.setCategory(articleCategoryService.getById(article.getCid()));
            articleVo.setCommentVos(commentService.findPageVo(new CommentQuery().setAid(article.getId())).getRecords());
            articleVos.add(articleVo);
        });
        return ResultBody.success(articleVos);
    }

    @GetMapping("/delById/{id}")
    public ResultBody delById(@PathVariable("id") int id){
        Boolean b = articleService.removeById(id);

        if(!b) return ResultBody.error("删除失败");
        return ResultBody.success();
    }

    @PostMapping("/add")
    public ResultBody add(@RequestBody Article article){
        Boolean b = articleService.save(article);
        if(!b) return ResultBody.error("添加失败");
        return ResultBody.success();
    }
    @PostMapping("/updateById")
    public ResultBody updateById(@RequestBody Article article){
        boolean b = articleService.updateById(article);
        if(!b) return ResultBody.error("更新失败");
        return ResultBody.success();
    }
}

